<?php include_once('Connections/tfccom_db.php'); ?>
<?php

if(isset($_POST['lastmsg']) &&is_numeric($_POST['lastmsg']))
{
mysql_select_db($database_tfccom_db, $tfccom_db);

$strsort = "";
if(!isset($_POST['sort'])){
	$_POST['sort']=4;
}
switch($_POST['sort']){
	case '1':// Sq Ft
		$strsort = "square_feet";
		break;
	case '2'://Beds
		$strsort = "num_bedrooms";
		break;
	case '3'://Baths
		$strsort = "num_bathrooms";
		break;
	default://price
		$strsort = "price";
		break;
}
if($strsort!="price"){
	$strsort = " order by apartment.".$strsort." ASC , price asc ";
}else{
	$strsort = " order by apartment.".$strsort." ASC ";
}
if($_POST['p']=='a')
	$prices = "";
else
{
	$search_prices=explode("-",$_POST['p']);
	$prices = " AND price >= ".$search_prices[0]." AND price <= ".$search_prices[1];
}

if($_POST['n']=='a')
	$Neighborhood = "";
else
	$Neighborhood = " AND neighborhood_id LIKE '".$_POST['n']."'";

if($_POST['be']=='a')
	$Bedrooms = "";
else
	$Bedrooms = " AND num_bedrooms = ".$_POST['be'];

if($_POST['ba']=='a')
	$Bathrooms = "";
else
	$Bathrooms = " AND num_bathrooms = ".$_POST['ba'];

if($_POST['bu']=='a')
	$Building = "";
else
	$Building = " AND building.address = '".$_POST['bu']."'";

if($_POST['am']=='a')
{
	$Amenities_1 = "";
	$Amenities_2 = "";
}
else
{
	$Amenities_1 = ", bldg_amenity_relation, amenity";
	$Amenities_2 = "AND bldg_amenity_relation.building_id = building.id AND bldg_amenity_relation.amenity_id=amenity.id AND amenity.id='".$_POST['am']."'";
}

if($_POST['a_s']=='a')
	$Apartment_Size = "";
else
	$Apartment_Size = " AND square_feet <= ".$_POST['a_s'];


$myQuery = sprintf("SELECT apartment.building_id, apartment.id,num_bedrooms, ROUND(num_bathrooms) AS num_bathrooms, apartment.apartment, apartment.price, apartment.square_feet, apartment.availability, building.neighborhood_id, building.name, building.contact_phone, building.contact_email, neighborhood.neighborhood,full_description, apt_type
FROM apartment, building, neighborhood %s
WHERE building.id = apartment.building_id AND neighborhood.id = building.neighborhood_id  AND apartment.availability =  '1' %s %s %s %s %s %s %s ".$strsort." LIMIT ".$_POST['lastmsg'].",5",$Amenities_1,$Neighborhood, $prices,$Bedrooms,$Bathrooms,$Building,$Apartment_Size,$Amenities_2);

$rsProperties = mysql_query($myQuery, $tfccom_db) or die(mysql_error());
$row_rsProperties = mysql_fetch_assoc($rsProperties);

$msg_id = $_POST['lastmsg']+5;
?>
<?php
 $lastid = "0"; 

do{
$query_rsThumbnail = "SELECT * FROM images WHERE property_id = '".$row_rsProperties['building_id']."' AND category = 'building' AND id != '".$lastid."' ORDER BY RAND() LIMIT 0,1";
$rsThumbnail = mysql_query($query_rsThumbnail, $tfccom_db) or die(mysql_error());
$row_rsThumbnail = mysql_fetch_assoc($rsThumbnail);
$totalRows_rsThumbnail = mysql_num_rows($rsThumbnail);

$lastid = $row_rsThumbnail['id'];

$BuildFileName = $row_rsProperties['name'];
$BuildFileName = str_replace(" ", "-", $BuildFileName);
$BuildFileName = str_replace(".", "", $BuildFileName);
	
?>
<div class='listing'><a href="unit_page.php?id=<?php echo $row_rsProperties['id']; ?>"><?php if ($totalRows_rsThumbnail!=0) { ?><div style=" background:url(http://www.tfcornerstone.com/images/building/<?php echo $row_rsThumbnail['file_name'];?>) no-repeat 50% 0; background-position:50% 50%;width:292px; height:210px;overflow:hidden; float:left"></div><?php } else { ?><img src="http://www.tfcornerstone.com/images/no-image-thumb.gif" width="292" height="210" border="0" alt="" /><?php } ?><div class='listing-info'>

<p class='first-line'><?php echo($row_rsProperties['name']) ?></p><p><?php echo($row_rsProperties['neighborhood']) ?></p><p><?php
		if($row_rsProperties['num_bedrooms']==0)
		{
		  	echo 'Studio';
		}
		else
		{
		   echo($row_rsProperties['num_bedrooms']) ?> BD<?php    }?> / <?php echo($row_rsProperties['num_bathrooms']) ?> BA</p><p>$<?php echo(number_format($row_rsProperties['price'])) ?>&nbsp;<?php echo($row_rsProperties['apt_type']) ?></p> 
</div></a>

<?php
$myQuery = "SELECT building.contact_phone 
FROM apartment, building 
WHERE building.id = apartment.building_id AND apartment.id='". $row_rsProperties['id'] ."'";
$rsPhone = mysql_query($myQuery, $tfccom_db) or die(mysql_error());
$row_rsPhone = mysql_fetch_assoc($rsPhone);
?>
<a href='tel:<?php echo $row_rsPhone['contact_phone']; ?>' class="phone2-button-title"><img src="img/phone2.gif" border="0"/></a>

</div><!-- .listing -->
</div><!-- .listing -->
 <?php } while ($row_rsProperties = mysql_fetch_assoc($rsProperties));
	
if(( mysql_num_rows($rsProperties)==5)&&($_POST['total']!=$msg_id)){
   ?> 
 	   <div id="more" class="more-results"><a href='#' class='more' id="<?php echo $msg_id; ?>">More</a></div>
<?php
 }
}
?>